var zuocedaohuang = document.querySelectorAll("#zuocedaohuang>ul>li>div");

//物联设备
$("#bs-example-navbar-collapse-1 ul:nth-child(3) li:nth-child(2) a").click(function () {
    $("body").load("WuJiaHanWuLianSheBei.html");
})
//主页
$("#zuocedaohuang>ul>li:nth-child(1)").click(function () {
    $("body").load("index.html");
})
//修改密码
$("#zuocedaohuang>ul>li:nth-child(8)>ul>li:first-child").click(function () {
    $("body").load("yaoketongxiugaimm.html");
})
$(".dropdown-menu>li:first-child").click(function () {
    $("body").load("yaoketongxiugaimm.html");
})
//人员管理
$("#zuocedaohuang>ul>li:nth-child(2)>ul>li").click(function () {
    $("body").load("WuJiaHanRenYuanGuanLi.html");
})
//初始化
$("#zuocedaohuang>ul>li:nth-child(8)>ul>li:last-child").click(function () {
    $("body").load("chushihua.html");
})
//主页
$("#bs-example-navbar-collapse-1 ul:nth-child(3) li:nth-child(1) a").click(function () {
    $("body").load("index.html");
})

//物业管理
$("#zuocedaohuang>ul>li:nth-child(5)>ul>li:first-child").click(function () {
    $("body").load("HeLuYiJiaoFeiJiLu.html");
})
//订单管理
$("#zuocedaohuang>ul>li:nth-child(5)>ul>li:nth-child(2)").click(function () {
    $("body").load("HeLuYiDaiJiaoDingDan.html");
})
//收款账号
$("#zuocedaohuang>ul>li:nth-child(5)>ul>li:nth-child(3)").click(function () {
    $("body").load("HeLuYiShouKuanZhangHao.html");
})

//房源管理
$("#zuocedaohuang>ul>li:nth-child(3)>ul>li:first-child").click(function () {
    $("body").load("renchengdongfangyuanhuanli.html");
})
//房屋档案
$("#zuocedaohuang>ul>li:nth-child(3)>ul>li:nth-child(2)").click(function () {
    $("body").load("renchengdongfangwudangan.html");
})

//车位管理
$("#zuocedaohuang>ul>li:nth-child(4)>ul>li:first-child").click(function () {
    $("body").load("cx-chewguanli.html");
})
//车辆管理
$("#zuocedaohuang>ul>li:nth-child(4)>ul>li:nth-child(2)").click(function () {
    $("body").load("cx-chelguanli.html");
})

//工单配置
$("#zuocedaohuang>ul>li:nth-child(6)>ul>li:first-child").click(function () {
    $("body").load("第一个页面.html");
})
//报修管理
$("#zuocedaohuang>ul>li:nth-child(6)>ul>li:nth-child(2)").click(function () {
    $("body").load("第二个页面.html");
})
//物业信息
$("#zuocedaohuang>ul>li:nth-child(7)>ul>li:nth-child(3)").click(function () {
    $("body").load("YaoKeTongWuYeXingXi.html");
})
//信息服务
$("#zuocedaohuang>ul>li:nth-child(7)>ul>li:first-child").click(function () {
    $("body").load("LiuXueQiXinXiFuWu.html");
})
//账单配置
$("#zuocedaohuang>ul>li:nth-child(7)>ul>li:nth-child(2)").click(function () {
    $("body").load("LiuXueQiZhangDanPeiZhi.html");
})

$(".dropdown-toggle").click(function () {
    if (navpanding==1){
        $(".dropdown-menu").css("display","block");
        $(".dropdown-toggle").css("background-color","rgb(221,221,221)")
        navpanding=2;
    }else {
        $(".dropdown-menu").css("display","none");
        $(".dropdown-toggle").css("background-color","rgb(65,92,133)")
        navpanding=1;
    }
})


for(var i = 0 ;i<zuocedaohuang.length;i++){
    zuocedaohuang[i].onclick=function () {
        var liClick = this.parentNode;
        var haizi=liClick.querySelectorAll("ul>li");
        var liHeight = liClick.style.height;
        for(var n = 1 ;n<zuocedaohuang.length;n++){
            var fa=[];
            fa[n]= zuocedaohuang[n].parentNode;
            fa[n].style.height="50px";
            fa[n].style.transition="all 0.2s";
            zuocedaohuang[n].style.color="black";
            zuocedaohuang[n].querySelector("span").style.transform="rotatez(0deg)";
        }
        if(liHeight=="50px"){
            if(haizi&&haizi.length>0){
                this.style.color="black";
                liClick.style.height=50+haizi.length*50+"px";
                this.querySelector("span").style.transform="rotatez(180deg)";
                liClick.style.transition="all 0.2s";
            }
        }else {
            this.style.color="black";
            liClick.style.height="50px";
            this.querySelector("span").style.transform="rotatez(0deg)";
            liClick.style.transition="all 0.2s";
            this.querySelector("span").style.transform="rotatez(0deg)";
        }
    }
}
$(window).scroll(function () {
    var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
    var i=scrollTop;
    if (scrollTop>500){
        $("#zuocedaohuang>ul").css("margin-top",scrollTop+"px");
    }
    if (scrollTop<500){
        $("#zuocedaohuang>ul").css("margin-top","0");
    }
})

var fymainLeft = document.querySelectorAll("#fymainLeft>ul>li>div");
for (var i = 0; i < fymainLeft.length; i++) {
    fymainLeft[i].onclick = function () {
        var liClick = this.parentNode;
        var haizi = liClick.querySelectorAll("ul>li");
        var liHeight = liClick.style.height;
        for (var n = 1; n < fymainLeft.length; n++) {
            var fa = [];
            fa[n] = fymainLeft[n].parentNode;
            fa[n].style.height = "35px";
            fa[n].style.transition = "all 0.2s";
            fymainLeft[n].style.color = "black";
        }
        if (liHeight == "35px") {
            if (haizi && haizi.length > 0) {
                this.style.color = "black";
                liClick.style.height = 35 + haizi.length * 30 + "px";
                liClick.style.transition = "all 0.2s";
            }
        } else {
            this.style.color = "black";
            liClick.style.height = "35px";
            liClick.style.transition = "all 0.2s";
        }
    }
}
$(window).scroll(function () {
    var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
    var i = scrollTop;
    if (scrollTop > 245) {
        $("#fymainLeft>ul").css("margin-top", scrollTop + "px");
    }
    if (scrollTop < 245) {
        $("#fymainLeft>ul").css("margin-top", "0")
    }
})





    var shanchuyemian = 1;

//存放所有用户
    var fangyuanusers = fangyuanusers || {};

//操作类型
    var operateType = "";
//存放搜索对象
    var searchUsers = searchUsers || {};
    var User = {
        Create: function (xh, xm, dh, xb, zz, zhlx) {
            this.xh = xh;
            this.xm = xm;
            this.dianhua = dh;
            this.xb = xb;
            this.zz = zz;
            this.zhlx = zhlx;
        },
        //添加用户
        addUserData: function () {
            if (this.xh != "") {
                fangyuanusers[this.xh] = this;
            }
        },
    }

    function New(aClass, aParams) {
        function new_() {
            aClass.Create.apply(this, aParams);
        }

        new_.prototype = aClass;
        return new new_();
    }

//bootstrap模态框事件
    $('#myModal').on('hide.bs.modal', function () {
        // 执行一些动作...
        var inputElements = this.getElementsByTagName("input");
        var userArr = [];
        for (var i = 0; i < inputElements.length; i++) {
            userArr[i] = inputElements[i].value;

            this.getElementsByTagName("input")[i].value = "";
        }
        var user = New(User, userArr);
        //添加操作
        if (operateType == "add") {
            user.addUserData();
            refreshDatas(fangyuanusers);
            operateType = "";
            yemashu();
            fanye1();
            shanchuyemian = 1;
            yemaheshuj();
        }
    });

    /**
     * 往表格添加一行html数据
     */
    function addRowData(datas) {
        var tbodyElement = document.getElementById("tbody");
        var html = "";
        for (var i in datas) {
            html = html + "<tr><th>"
                + datas[i].xh + "</th><th>"
                + datas[i].xm + "</th><th>"
                + datas[i].dianhua + "</th><th>"
                + datas[i].xb + "</th><th>"
                + datas[i].zz + "</th><th>"
                + datas[i].zhlx + "</th><th><button data-toggle=\"modal\" data-target=\"#myModal\" onclick='bianji(this)'>编辑</button> <button onclick='shanchu(this)'>删除</button></th></tr>";
        }
        tbodyElement.innerHTML = html;
    }

    /**
     * 刷新用户数据
     */
    function refreshDatas(datas) {
        addRowData(datas);
    }

    function initUserDatas() {
        var initUser1 = New(User, ["1", "101", "1", "一", "01#", "A区(—期)"]);
        var initUser2 = New(User, ["2", "102", "1", "一", "01#", "A区(—期)"]);
        var initUser3 = New(User, ["3", "103", "1", "一", "04#", "A区(—期)"]);
        var initUser4 = New(User, ["4", "B01", "B1", "一", "公共区域", "A区(—期)"]);
        var initUser5 = New(User, ["5", "201", "2", "一", "02#", "A区(—期)"]);
        var initUser6 = New(User, ["6", "202", "2", "一", "02#", "A区(—期)"]);
        var initUser7 = New(User, ["7", "B02", "B1", "一", "公共区域", "A区(—期)"]);
        var initUser8 = New(User, ["8", "205", "2", "一", "02#", "A区(—期)"]);
        var initUser9 = New(User, ["9", "301", "3", "一", "03#", "A区(—期)"]);
        var initUser10 = New(User, ["10", "302", "3", "一", "03#", "A区(—期)"]);
        var initUser11 = New(User, ["11", "401", "4", "一", "04#", "A区(—期)"]);
        var initUser12 = New(User, ["12", "B03", "B1", "一", "公共区域", "A区(—期)"]);
        var initUser13 = New(User, ["13", "501", "5", "一", "05#", "A区(—期)"]);
        var initUser14 = New(User, ["14", "108", "1", "一", "07#", "A区(—期)"]);
        var initUser15 = New(User, ["15", "504", "5", "一", "05#", "A区(—期)"]);
        var initUser16 = New(User, ["16", "B04", "B1", "一", "公共区域", "A区(—期)"]);
        var initUser17 = New(User, ["17", "606", "6", "一", "06#", "A区(—期)"]);
        var initUser18 = New(User, ["18", "605", "6", "一", "07#", "A区(—期)"]);
        var initUser19 = New(User, ["19", "306", "3", "一", "03#", "A区(—期)"]);
        var initUser20 = New(User, ["20", "603", "6", "一", "06#", "A区(—期)"]);
        fangyuanusers[initUser1.xh] = initUser1;
        fangyuanusers[initUser2.xh] = initUser2;
        fangyuanusers[initUser3.xh] = initUser3;
        fangyuanusers[initUser4.xh] = initUser4;
        fangyuanusers[initUser5.xh] = initUser5;
        fangyuanusers[initUser6.xh] = initUser6;
        fangyuanusers[initUser7.xh] = initUser7;
        fangyuanusers[initUser8.xh] = initUser8;
        fangyuanusers[initUser9.xh] = initUser9;
        fangyuanusers[initUser10.xh] = initUser10;
        fangyuanusers[initUser11.xh] = initUser11;
        fangyuanusers[initUser12.xh] = initUser12;
        fangyuanusers[initUser13.xh] = initUser13;
        fangyuanusers[initUser14.xh] = initUser14;
        fangyuanusers[initUser15.xh] = initUser15;
        fangyuanusers[initUser16.xh] = initUser16;
        fangyuanusers[initUser17.xh] = initUser17;
        fangyuanusers[initUser18.xh] = initUser18;
        fangyuanusers[initUser19.xh] = initUser19;
        fangyuanusers[initUser20.xh] = initUser20;
        return fangyuanusers;
    }

    /**
     * 首次加载页面执行方法
     */
        // function loadUserDatas() {
    var userArray = initUserDatas();
    addRowData(userArray);
    refreshDatas(fangyuanusers);
    // fanye1();
    yemashu();
    fanye1();
    // }

//用户操作
    function add(param) {
//获得操作类别
        document.getElementById("myModalLabel").innerText = "添加用户";
        operateType = "add";
    }

//删除
    function shanchu(odj) {
        var i = parseInt($(odj).parent().parent().find("th")[0].innerText);
        delete fangyuanusers[i];
        $(odj).parent().parent().remove();
        yemashu();
        fanye2();

        yemaheshuj();
    }

//编辑
    function bianji(odj) {
        var i = parseInt($(odj).parent().parent().find("th")[0].innerText);
        var modal_body = document.getElementById("modal-body");
        var inputElements = modal_body.getElementsByTagName("input");
        document.getElementById("myModalLabel").innerText = "修改用户";
        inputElements[0].value = fangyuanusers[i].xh;
        inputElements[1].value = fangyuanusers[i].xm;
        inputElements[2].value = fangyuanusers[i].dianhua;
        inputElements[3].value = fangyuanusers[i].xb;
        inputElements[4].value = fangyuanusers[i].zz;
        inputElements[5].value = fangyuanusers[i].zhlx;
        operateType = "add";
    }

//查询
// var fymainLeftCha = document.querySelectorAll("#fymainLeft>ul>li>ul>li");

    $("#fymainLeft>ul>li>ul>li>span").click(function () {
        for (var i in fangyuanusers){
            var text=$(this).text()+"";
            // console.log(text);
            if( text === fangyuanusers[i].zz){
                searchUsers[fangyuanusers[i].xh] = fangyuanusers[i];
                refreshDatas(searchUsers);
            }

        }
        searchUsers = {};
        yemashu();
        fanye2();
        shanchuyemian = 1;
        yemaheshuj();
    })


//页面数
    function yemashu() {
        var yema = $(".yema");
        var neirong = $("#profile #tbody tr");
        var yema1 = Math.ceil(neirong.length / 8);
        var html = "";
        if (yema1 > 0) {
            for (var i = 1; i <= yema1; i++) {
                html = html + "<button onclick='fanye(this)'>" + i + "</button>";
            }
            yema[0].innerHTML = html;
            html = "";
        }
    }

//翻页
    function fanye(odj) {
        var neirong = $("#profile #tbody tr");
        neirong.css("display", "none");
        shanchuyemian = odj.innerText;
        for (var i = 0; i < neirong.length; i++) {
            if (i >= odj.innerText * 8 - 8 && i < odj.innerText * 8) {
                neirong[i].style.display = "table-row";
            }
        }
        yemaheshuj();
    }

//加载翻页
    function fanye1() {
        var neirong = $("#profile #tbody tr");
        neirong.css("display", "none");
        for (var i = 0; i < 8; i++) {
            if (i >= 0 && i < 8) {
                neirong[i].style.display = "table-row";
            }
        }
    }

    function fanye2() {
        var neirong = $("#profile #tbody tr");
        neirong.css("display", "none");
        for (var i = 0; i < neirong.length; i++) {
            if (i >= shanchuyemian * 8 - 8 && i < shanchuyemian * 8) {
                neirong[i].style.display = "table-row";
            }
        }
    }

    function yemaheshuj() {
        var yemaheshujun = $(" .yemaheshujun");
        var neirong = $("#profile #tbody tr");
        yemaheshujun.text("当前页面：" + shanchuyemian + " 一共" + neirong.length + "条数据")
    }

// $("#chongzhi a").click(function (e) {
//     e.preventDefault();
//     $("body").load(e.target.href);
// })


